package indi.xielin.start.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.*;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MybatisPlus配置类
 *  @author  xielin
 *  @date  2022/10/17  0:06
 */

@Configuration
public class MybatisPlusConfig {

    /**
     * 拦截器配置,可以添加mybatisPlus各类内置拦截器
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());//添加乐观锁插件
//        interceptor.addInnerInterceptor(tenantLineInnerInterceptor());//添加租户插件
        interceptor.addInnerInterceptor(paginationInnerInterceptor());//添加分页插件
//        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor());//添加动态表名插件
//        interceptor.addInnerInterceptor(blockAttackInnerInterceptor());//禁止全局更新删除插件
        return interceptor;
    }

    /**
     * 乐观锁插件
     * @return
     */
    private OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor(){
        return new OptimisticLockerInnerInterceptor();
    }

    /**
     * 动态表插件
     * 在TableNameHandler类中在dynamicTableName方法中返回表名，传递进去的参数为sql语句，以及原tableName
     */
    private DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor(){
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        dynamicTableNameInnerInterceptor.setTableNameHandler((sql,tableName)->{
            //return dynamicTableName
            return "tableName_01";//在这里返回动态表名，亲测有效
        });
        return dynamicTableNameInnerInterceptor;
    }

    /**
     * 防全局更新删除插件
     * @return
     */
    private BlockAttackInnerInterceptor blockAttackInnerInterceptor(){
        return new BlockAttackInnerInterceptor();
    }

    /**
     * 分页插件
     */
    private PaginationInnerInterceptor paginationInnerInterceptor(){
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        return paginationInnerInterceptor;
    }

    /**
     *  租户插件
     */
    private TenantLineInnerInterceptor tenantLineInnerInterceptor(){
        TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor();
        tenantLineInnerInterceptor.setTenantLineHandler(new TenantLineHandler() {
            /**
             * 获取租户 ID 值表达式，只支持单个 ID 值
             * @return 租户 ID 值表达式
             */
            @Override
            public Expression getTenantId() {//返回自定义租户ID，在Sql中会拼接where 租户id=此方法id  获取租户 ID 值表达式，只支持单个 ID 值
                return new StringValue("白色");
            }

            /**
             * 获取租户字段名
             * <p>
             * 默认字段名叫: tenant_id
             *
             * @return 租户字段名
             */
            @Override
            public String getTenantIdColumn() {
                return "car_color";
            }

            /**
             * 根据表名判断是否忽略拼接多租户条件
             * <p>
             * 默认都要进行解析并拼接多租户条件
             *
             * @param tableName 表名
             * @return 是否忽略, true:表示忽略，false:需要解析并拼接多租户条件
             */
            @Override
            public boolean ignoreTable(String tableName) {
                if ("role".equals(tableName.toString())){
                    //举例：-如果是role表，忽略租户，不拼接条件
                    return true;
                }
                return false;
            }

        });
        return tenantLineInnerInterceptor;
    }

}
